const mongoose = require('mongoose');
const pagination = require('mongoose-sex-page');

const { Article } = require('../../model/article.js')

module.exports = async (req, res) => {
    var keywords = req.query.keywords
    var { page } = req.query.page
    if (page == undefined) {
        page = 1
    }
    // console.log(page);
    // console.log(keywords);
    try {
        var data = await pagination(Article).find({ $or: [{ title: { $regex: keywords, $options: "$i" } }, { label: { $in: [keywords] } }, { content: { $regex: keywords, $options: "$i" } }] }).page(page).size(7).exec()
    } catch (err) {
        console.log(err);
        return res.send({
            data: null,
            meta: {
                status: 400,
                msg: '请求失败'
            }
        })
    }
    res.send({
        data,
        meta: {
            status: 200,
            msg: '请求成功'
        }
    })
}